<?php

namespace app\admin\controller\ykjp\output;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\ykjp\product\Product as products;
use app\admin\model\Admin;
use app\admin\model\ykjp\information\basisinfo\Customerinfo;
use app\admin\model\ykjp\sell\Sell as SellModel;

/**
 * 出库数据导出
 *
 * @icon fa fa-circle-o
 */
class Sell extends Backend {

    /**
     * Purchase模型对象
     * @var \app\admin\model\ykjp\purchase\Storage
     */
    protected $model = null;
    protected $searchFields = "name";
    protected $distinguish = true;
    protected $wherename = "";

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\sell\deliveryPro;
    }

    /**
     * 查看
     */
    public function index() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $this->wherename = "delivery.firmid";
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['delivery'])
                    ->where($where)
                    ->where("delivery.status_list", "6")
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->with(['delivery'])
                    ->where($where)
                    ->where("delivery.status_list", "6")
                    ->order($sort, $order)
                    ->select();
            $Admin = new Admin;
            $prodcut = new products;
            $Customerinfo = new Customerinfo;
            $Model = new SellModel;
            foreach ($list as $key => $row) {
                $row['out_type'] = "Type 0";
                $sellAdminId = $Model->where("id", $row['delivery']['sell_id'])->Field("admin_id")->find();
                if ($sellAdminId) {
                    $username = $Admin->where("id", $sellAdminId['admin_id'])->Field("nickname")->find();
                }
                $row['prep_name'] = isset($username['nickname']) ? $username['nickname'] : "";
                $productName = $prodcut->where("id", $row['product_id'])->Field("name")->find();
                $row['name'] = isset($productName['name']) ? $productName['name'] : "";
                $custoInfo = $Customerinfo->where("id", $row['delivery']['customer_info_id'])->find();
                $row['custo'] = isset($custoInfo['name']) ? $custoInfo['name'] : "";
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
